package com.transaction.utils;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.Properties;
import java.util.logging.Logger;

public class MyPool implements DataSource {
    private static Properties props = new Properties();
    private static LinkedList<Connection> list = new LinkedList<>();

    static {
        InputStream in = MyPool.class.getResourceAsStream("dbconfig.properties");
        try {
            props.load(in);
            Class.forName(props.getProperty("driverClassName"));
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public MyPool(int initialCapacity) throws Exception {
        if (initialCapacity <= 0) {
            throw new Exception("给的初始化容量值不符合要求");
        }
        for (int i = 0; i < initialCapacity; i++) {
            Connection con = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"));
            MyConnection myConnection = new MyConnection(con);
            list.add(myConnection);
        }
    }

    public static void backConnection(Connection connection) {
        list.addLast(connection);
    }

    @Override
    public Connection getConnection() throws SQLException {
        return null;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}
